/**
 * @file simple_flatbuffer_example.cpp
 * @brief 简单的FlatBuffer序列化器使用示例
 * 展示如何实现类似Protobuf的调用方式
 */

#include <iostream>
#include "serializer/flatbuffer_serializer.h"
#include "serializer/user_flatbuffer_traits.h"

using namespace flatbuffers::serializer;

int main() {
    std::cout << "=== FlatBuffer序列化器简单示例 ===" << std::endl;
    
    try {
        // 1. 创建序列化器
        FlatBufferSerializer serializer;
        std::cout << "序列化器: " << serializer.name() 
                  << " v" << serializer.version() << std::endl;
        
        // 2. 创建数据 - 类似创建protobuf消息
        UserData user{12345, "张三", "zhangsan@example.com", 95, true};
        std::cout << "\n原始数据:" << std::endl;
        std::cout << "  用户ID: " << user.id << std::endl;
        std::cout << "  姓名: " << user.name << std::endl;
        std::cout << "  邮箱: " << user.email << std::endl;
        std::cout << "  分数: " << user.score << std::endl;
        std::cout << "  状态: " << (user.active ? "活跃" : "非活跃") << std::endl;
        
        // 3. 序列化 - 类似protobuf的SerializeToString
        //    注意：需要指定数据类型和FlatBuffer类型
        auto serialized_data = serializer.serialize<UserData, comm::User>(user);
        std::cout << "\n序列化成功!" << std::endl;
        std::cout << "序列化后大小: " << serialized_data.size() << " bytes" << std::endl;
        
        // 4. 反序列化 - 类似protobuf的ParseFromString
        UserData restored_user;
        bool success = serializer.deserialize<UserData, comm::User>(serialized_data, restored_user);
        
        if (!success) {
            std::cout << "反序列化失败!" << std::endl;
            return -1;
        }
        
        std::cout << "\n反序列化成功!" << std::endl;
        std::cout << "恢复的数据:" << std::endl;
        std::cout << "  用户ID: " << restored_user.id << std::endl;
        std::cout << "  姓名: " << restored_user.name << std::endl;
        std::cout << "  邮箱: " << restored_user.email << std::endl;
        std::cout << "  分数: " << restored_user.score << std::endl;
        std::cout << "  状态: " << (restored_user.active ? "活跃" : "非活跃") << std::endl;
        
        // 5. 验证数据完整性
        bool data_matches = (user.id == restored_user.id &&
                           user.name == restored_user.name &&
                           user.email == restored_user.email &&
                           user.score == restored_user.score &&
                           user.active == restored_user.active);
        
        std::cout << "\n数据完整性检查: " 
                  << (data_matches ? "✓ 通过" : "✗ 失败") << std::endl;
        
        // 6. 演示零拷贝访问（FlatBuffer的优势）
        const comm::User* fb_user = nullptr;
        success = serializer.deserialize<UserData, comm::User>(serialized_data, fb_user);
        
        if (success && fb_user) {
            std::cout << "\n零拷贝访问:" << std::endl;
            std::cout << "  直接从FlatBuffer读取姓名: " << fb_user->name()->c_str() << std::endl;
            std::cout << "  直接从FlatBuffer读取分数: " << fb_user->score() << std::endl;
        }
        
        std::cout << "\n=== 示例完成 ===" << std::endl;
        return 0;
        
    } catch (const std::exception& e) {
        std::cerr << "异常: " << e.what() << std::endl;
        return -1;
    }
}
